import dayjs, { Dayjs } from 'dayjs'
import { FormSchema } from '../Form'

export const dateOptions = [
  { label: '月', value: 'month', picker: 'month', isRange: false, format: 'YYYY-MM' },
  { label: '年', value: 'year', picker: 'year', isRange: false, format: 'YYYY' },
  // { label: '自定义', value: 'custom', picker: 'date', isRange: true, format: 'YYYY-MM-DD' },
  // { label: '历年', value: 'hisYear', picker: 'year', isRange: true, format: 'YYYY' },
]

const pickerObj = {
  day: 'day',
  seven: 'custom',
  thirty: 'custom',
  hisYear: 'hisYear',
  custom: 'custom',
  season: 'custom',
  week: 'week',
  year: 'year',
  halfyear: 'custom',
}
export const pickerType = (value) => {
  const buttonValue = (value || 'day') as keyof typeof pickerObj
  return pickerObj[buttonValue] || buttonValue
}
export const setDisabledDate = (current: Dayjs, maxDate) => {
  maxDate = maxDate || dayjs().endOf('day')
  return current && current > maxDate
}
	
export const searchFormSchema: FormSchema[] = [
	{
		field: 'queryType',
		label: '时间粒度',
		component: 'Select',
		colProps: { span: 4 },
		defaultValue: 'month',
		componentProps: ({formModel}) => ({
			options: [
				{
					label: '月',
					value: 'month',
					key: 'month'
				},
				{
					label: '年',
					value: 'year',
					key: 'year'
				}
			],
			onChange: (value) =>{
				formModel['[startDay,endDay]'] = ['', '']
        const { format } = dateOptions.filter(
          (item) => item.value === value,
        )[0]
        const defaultMaxDate = dayjs()
        switch (value) {
          case 'custom':
            // 计算当前日期往前7天到昨天的时间点
            const sixDaysAgo = defaultMaxDate.subtract(7, 'days').format(format)
            const lastDayAgo = defaultMaxDate.subtract(1, 'days').format(format)
            formModel['[startDay,endDay]'] = [sixDaysAgo, lastDayAgo]
            break
        }
			}
		})
	},
	{ 
		label: '月',
		field: '[startDay,endDay]',
		colProps: { span: 6 },
		show: ({values}) => values.queryType == 'month',
		component: 'RangePicker',
		componentProps: {
			picker: 'month',
			valueFormat: 'YYYY-MM',
			placeholder: ['月', '月'],
	 	}
	},
	{ label: '年',
		field: '[startDay,endDay]',
		component: 'RangePicker',
		colProps: { span: 6 },
		show: ({values}) => values.queryType == 'year',
		componentProps: {
			picker: 'year',
			valueFormat: 'YYYY',
			placeholder: ['年', '年']
		}
	}
]
export const getSearchFormSchema1 = ({ disabledOption = [''], chooseMaxDate = {} }) => {
  const filterOptions = dateOptions
  const initData = filterOptions[1]
  // if (disabledOption.length > 0) {
  //   disabledOption.forEach((dItem) => {
  //     filterOptions = filterOptions.filter((item) => item.value !== dItem)
  //   })
  // }

  // picker为周的情况下，defaultValue赋值失败，暂时绕过处理
  // if (filterOptions[0].value === 'week') {
  //   initData = filterOptions[1]
  // }

  return [
    {
      field: 'queryType',
      label: '时间粒度',

      component: 'Select',
      colProps: { span: 4 },
      defaultValue: initData.value,
      componentProps: ({ formModel, formActionType }) => {
        return {
          options: filterOptions,
          allowClear: false,
          onChange: (val) => {
            const { isRange, format, picker, value } = filterOptions.filter(
              (item) => item.value === val,
            )[0]
            const { updateSchema } = formActionType
            const defaultMaxDate = chooseMaxDate[val as any] || dayjs()
            const commonProps = {
              picker,
              disabledDate: (current) => setDisabledDate(current, defaultMaxDate),
              valueFormat: format,
            }
            const defaultDate = defaultMaxDate.format(format)
            // 处理周、季、半年、年的默认值
            switch (value) {
              case 'week':
                break
              case 'seven':
                // 计算当前日期往前6天的时间点
                const sixDaysAgo = defaultMaxDate.subtract(6, 'days').format(format)
                formModel['[startDay,endDay]'] = [sixDaysAgo, defaultDate]
                break
              case 'thirty':
                // 计算当前日期往前30天的时间点
                const thirtyDaysAgo = defaultMaxDate.subtract(30, 'days').format(format)
                formModel['[startDay,endDay]'] = [thirtyDaysAgo, defaultDate]
                break
              case 'season':
                // 计算当前时间处于第几个季度
                const currentQuarter = defaultMaxDate.quarter()
                // 计算当前季度的开始时间
                const startOfQuarter = defaultMaxDate.startOf('quarter').format(format)
                // 计算当前季度的结束时间
                const endOfQuarter = defaultMaxDate.endOf('quarter').format(format)
                formModel['[startDay,endDay]'] = [startOfQuarter, endOfQuarter]
                break
              case 'halfyear':
                // 计算当前日期往前6个月的时间点
                const sixMonthAgo = defaultMaxDate.subtract(6, 'month').format(format)
                formModel['[startDay,endDay]'] = [sixMonthAgo, defaultDate]
                break
              case 'custom':
                // 计算当前日期往前7天到昨天的时间点
                const startDaysAgo = defaultMaxDate.subtract(30, 'days').format(format)
                formModel['[startDay,endDay]'] = [startDaysAgo, defaultDate]
                break
              default:
                delete formModel['[startDay,endDay]']
                break
            }
            formModel.startDay = defaultDate
            updateSchema([
              {
                field: 'startDay',
                show: !isRange,
                colProps: { span: 6 },
                componentProps: {
                  ...commonProps,
                },
              },
              {
                field: '[startDay,endDay]',
                show: isRange,
                colProps: { span: 6 },
                componentProps: {
                  ...commonProps,
                },
              },
            ])
          },
        }
      },
    },
    {
      field: 'startDay',
      label: '开始时间',
      component: 'DatePicker',
      show: false,
      colProps: { span: 6 },
      defaultValue: chooseMaxDate[initData.value] || dayjs(),
      componentProps: {
        picker: pickerType(initData.picker),
        valueFormat: initData.format,
        disabledDate: (current) => setDisabledDate(current, chooseMaxDate[initData.value]),
      },
    },
    {
      field: '[startDay,endDay]',
      label: '开始时间',
      colProps: { span: 6 },
      component: 'RangePicker',
      show: false,
    },
  ] as FormSchema[]
}
